package com.mojito.learn.algorithm.interview;

import com.mojito.learn.algorithm.datastructure.TreeNode;

import java.util.Scanner;

/**
 * @author liufengqiang
 * @date 2021-04-23 21:02:34
 */
public class HuaweiTest1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        HuaweiTest1 main = new HuaweiTest1();

        TreeNode treeNode = TreeNode.build(input);
        System.out.println(main.topic0(treeNode));
    }

    public String topic0(TreeNode root) {
        if (root.left == null && root.right == null) {
            return String.valueOf(root.val);
        }

        StringBuilder sb = new StringBuilder();
        getChild(root, sb);
        return sb.toString().substring(1);
    }

    private void getChild(TreeNode node, StringBuilder sb) {

        if (node == null || (node.left == null && node.right == null)) {
            return;
        }
        getChild(node.left, sb);
        getChild(node.right, sb);
        sb.append(" ").append(node.val);
    }
}
